package warthog.scangen;

import java.util.*;


public class Node {
	private final List<Node> epsilon = new LinkedList<Node>();
	public void eps(Node that) {
		if (null==that) throw new IllegalArgumentException();
		epsilon.add(that);
	}
	private final List<Edge> edge = new LinkedList<Edge>();
	public void edge(Edge e) {edge.add(e);}
	Rule rule;
	public static Node Terminal(Rule rule) {
		Node n = new Node();
		n.rule = rule;
		return n;
	}
	public static void close(Set<Node> set) {
		List<Node> work = new LinkedList<Node>(set);
		while (!work.isEmpty()) {
			Node focus = work.remove(0);
			Collection<Node> eps = focus.epsilon;
			for (Node target:eps) if (!set.contains(target)) {
				set.add(target);
				work.add(target);
			}
		}
	}
	public Collection<Character> boundList() {
		Collection<Character> foo = new TreeSet<Character>();
		for (Edge e:edge) {
			foo.addAll(e.bounds());
		}
		return foo;
	}
	public Collection<Node> after(Character ch) {
		Set<Node> successor = new HashSet<Node>();
		for (Edge e:edge) e.updateSubset(successor, ch);
		return successor;
	}
}
